package com.jinlejia.erp.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by Administrator on 2016/10/20 0020.
 */

public class DateUtil {

    public static String getTime(long time) {
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");
        return myFmt.format(new Date(time));
    }

    public static String getTime(String time) {
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return myFmt.format(new Date(Long.valueOf(time)));
    }

    public static String getTime(long time, String fomat) {
        SimpleDateFormat myFmt = new SimpleDateFormat(fomat);
        return myFmt.format(new Date(time));
    }

    public static String getTime(String time, String fomat) {
        SimpleDateFormat myFmt = new SimpleDateFormat(fomat);
        if (time.equals("")) time = "0";
        return myFmt.format(new Date(Long.valueOf(time)));
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //获取该时间戳当天0点
    public static String getTimeStart(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return getTime(cal.getTimeInMillis(), "yyyy-MM-dd HH:mm:ss");
    }

    //获取该时间戳23点59
    public static String getTimeEnd(long time) {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTimeInMillis(time);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.MILLISECOND, 0);
        return getTime(todayEnd.getTimeInMillis(), "yyyy-MM-dd HH:mm:ss");
    }

    //获得当天0点时间
    public static String getDateTimeStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return getTime(cal.getTimeInMillis());
    }

    //获得当天24点时间
    public static String getDateEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.MILLISECOND, 0);
        return getTime(todayEnd.getTimeInMillis());
    }

    //获得当月第一天0点时间
    public static String getMonthTimeStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return getTime(cal.getTimeInMillis());
    }

    //获得当月最后一天24点时间
    public static String getMonthEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.DAY_OF_MONTH, todayEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.MILLISECOND, 0);
        return getTime(todayEnd.getTimeInMillis());
    }

    /**
     * 获取前一天
     *
     * @param time
     * @return
     */
    public static long previousDay(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int date = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, date - 1);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取下一天
     *
     * @param time
     * @return
     */
    public static long nextDay(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int date = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, date + 1);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取几月几日星期几
     *
     * @param time
     * @return
     */
    public static String getDateWeek(long time) {

        String mMonth, mDay, mWay;
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        c.setTimeInMillis(time);
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mMonth + "月" + mDay + "日" + " 星期" + mWay;

    }

}
